package com.chenwenxuan.sync.console.dao;

import static com.chenwenxuan.sync.utils.Commons.intToBool;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.chenwenxuan.sync.console.model.Task;
import com.chenwenxuan.sync.console.model.TaskArg;
import com.chenwenxuan.sync.console.model.TaskMail;
import com.chenwenxuan.sync.console.model.TaskCatalog;

public abstract class RowMappers {
	
	public static class TaskRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowIndex) throws SQLException {
			
			String taskId = rs.getString("TASK_ID");
			String taskName = rs.getString("TASK_NAME");
			String taskType = rs.getString("TASK_TYPE");
			String freq = rs.getString("FREQ");
			String startTime = rs.getString("START_TIME");
			String lastExecute = rs.getString("LAST_EXECUTE");
			String cronExpression = rs.getString("CRONEXPRESSION");
			int scheduleType = rs.getInt("SCHEDULE_TYPE");
			int executeOrder = rs.getInt("EXECUTE_ORDER");
			int debugEnable = rs.getInt("debug_enable");
			int testMode = rs.getInt("TEST_ENABLE");
			int maxRetry = rs.getInt("MAX_RETRY");
			int retryType = rs.getInt("RETRY_TYPE");
			int condType = rs.getInt("COND_TYPE");
			String condText = rs.getString("COND_TEXT");
			String executeGroup = rs.getString("EXECUTE_GROUP");
			int catalogId = rs.getInt("CATALOG_ID");
			
			Task task = new Task();
			
			task.setTaskName(taskName);
			task.setTaskType(taskType);
			task.setFreq(freq);
			task.setStartTime(startTime);
			task.setTaskId(new Integer(taskId));
			task.setLastExecute(lastExecute);
			task.setDebugEnable( intToBool(debugEnable) );
			task.setTestEnable( intToBool(testMode) );
			task.setCronExpression(cronExpression);
			task.setScheduleType(scheduleType);
			task.setMaxRetry(maxRetry);
			task.setRetryType(retryType);
			task.setCondType(condType);
			task.setCondText(condText);
			task.setExecuteOrder(executeOrder);
			task.setExecuteGroup(executeGroup);
			task.setCatalogId(catalogId);
			
			return task;
		}
		
	}

	public static class TaskArgRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowIndex) throws SQLException {

			String argName = rs.getString("ARG_NAME");
			String argValue = rs.getString("ARG_VALUE");
			int argId = rs.getInt("ARG_ID");
			String argType = rs.getString("ARG_TYPE");
			int argOrder = rs.getInt("ARG_ORDER");
			int taskId = rs.getInt("TASK_ID");

			TaskArg arg = new TaskArg();
			arg.setArgName(argName);
			arg.setArgValue(argValue);
			arg.setArgId(argId);
			arg.setArgType(argType);
			arg.setArgOrder(argOrder);
			arg.setTaskId(taskId);
			return arg;
		}
		
	}

	public static class TaskMailRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowIndex) throws SQLException {
			
			int taskId = rs.getInt("TASK_ID");
			String msgTemplate = rs.getString("MSG_TEMPLATE");
			String receiver = rs.getString("RECEIVER");
			String [] mailList;
			
			if ( receiver == null || receiver.isEmpty() ) {
				mailList = new String[0];
			} else  {
				mailList = receiver.split(";");
			}
			
			String msgTitle = rs.getString("MSG_TITLE");
			int enable = rs.getInt("ENABLE");
			
			return new TaskMail(taskId, mailList, msgTemplate, msgTitle, intToBool(enable));
		}
		
	}
	
	public static class TaskTypeUIRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowIndex) throws SQLException {
			
			int taskCatalogId = rs.getInt("CATALOG_ID");
			String taskCatalogName = rs.getString("CATALOG_NAME");
			int parentTaskCatalogId = rs.getInt("PARENT_CATALOG_ID");
			
			TaskCatalog taskCatalog = new TaskCatalog();
			taskCatalog.setTaskCatalogId(taskCatalogId);
			taskCatalog.setTaskCatalogName(taskCatalogName);
			taskCatalog.setParentTaskCatalogId(parentTaskCatalogId);
			
			return taskCatalog;
		}
		
	}
	
}
